<template>
    <ContentFieldVue v-if="!$store.state.user.pulling_info">
        <div class="row justify-content-md-center">  <!-- 全文检索ctrl + f -->
            <div class="col-3">
                <form @submit.prevent="login"> <!-- 将提交和登陆绑定 -->
                    <div class="mb-3">
                        <label for="username" class="form-label">用户名</label>
                        <input v-model="username" type="text" class="form-control" id="username" placeholder="请输入用户名">
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label">密码</label>
                        <input v-model="password" type="password" class="form-control" id="password" placeholder="请输入密码">
                    </div>
                    <div class="error-message">{{ error_message }}</div>
                    <button type="submit" class="btn btn-primary">提交</button>
                </form>
               <!--  <div style="text-align: center; margin-top: 20px; cursor: pointer;" @click="acwing_login">
                    <img  width="30" src="https://cdn.acwing.com/media/article/image/2022/09/06/1_32f001fd2d-acwing_logo.png" alt="">
                    <br>
                    AcWing一键登录
                </div> -->
            </div>
        </div>
    </ContentFieldVue>
</template>

<script>
import ContentFieldVue from "@/components/ContentField.vue";
import { useStore } from 'vuex' // 全局变量
import { ref } from 'vue'
import router from "@/router";
import $ from 'jquery'

export default {
    components: {
        ContentFieldVue
    },
    setup() {
        const store = useStore();
        let username = ref('');
        let password = ref('');
        let error_message = ref('');


        const jwt_token = localStorage.getItem("jwt_token");
        if (jwt_token) {
            store.commit("updateToken", jwt_token);
            store.dispatch("getinfo", {
                success() {
                    router.push({ name: "home" });
                    store.commit("updatePullingInfo", false);
                },
                error() {
                    store.commit("updatePullingInfo", false);
                }
            })
        } else {
            store.commit("updatePullingInfo", false);
        }

        const login = () => {
            error_message.value = "";
            store.dispatch("login", {  //调用action里的函数要用dispatch
                username: username.value,
                password: password.value,
                success() {
                    store.dispatch("getinfo", {
                        success() {
                            router.push({ name: 'home' });
                            console.log(store.state.user);
                        }
                    })
                },
                error() {
                    error_message.value = "用户名或密码错误";
                }
            }) 
        }

        const acwing_login = () => {
            $.ajax({
                url: "https://app7167.acapp.acwing.com.cn/api/user/account/acwing/web/apply_code/",
                type: "GET",
                success: resp => {
                    console.log(resp);
                    if (resp.result === "success") {
                        window.location.replace(resp.apply_code_url);
                    }
                }
            })
        }

        return {
            username,
            password,
            error_message,
            login,
            acwing_login,
        }
    }
}
</script>

<style scoped>
button {
    width: 100%;
}
div.error-message {
    color: red;
}
</style>